//
//  FLNAdViewController.m
//  百思不得姐
//
//  Created by xmg on 16/5/31.
//  Copyright © 2016年 FLN.com. All rights reserved.
//

#import "FLNAdViewController.h"
//#import <AFNetworking.h>
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "FLNAdItem.h"
#import <UIImageView+WebCache.h>
#import "FLNViewController.h"
#define FLNCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"


@interface FLNAdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *bgView;
@property (weak, nonatomic) IBOutlet UIView *containView;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property (nonatomic, strong) FLNAdItem *item;
@property (nonatomic, weak) NSTimer *timer;
@end

@implementation FLNAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpBG];
    
    [self loadADData];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    
}

- (void)timeChange {
    static int i = 3;
    i--;
    
    //修改按钮文字
    NSString *title = [NSString stringWithFormat:@"跳过 (%d)", i];
    [_jumpBtn setTitle:title forState:UIControlStateNormal];
    
    if (i == -1) {
        [self jump];
    }
}

- (void)loadADData {
    //创建会话管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    //拼接请求参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"code2"] = FLNCode2;
    
    //发送请求
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //请求成功
        [responseObject writeToFile:@"/Users/xmg/Desktop/baisi/baisibudejie/百思不得姐/百思不得姐 " atomically:YES];
        
        //获取广告字典
        NSDictionary *adDict = [responseObject[@"ad"] firstObject];
        //字典转模型
        FLNAdItem *item = [FLNAdItem mj_objectWithKeyValues:adDict];
        _item = item;
        //把广告数据展示到广告界面
        UIImageView *adImageView = [[UIImageView alloc] init];
        [self.containView addSubview:adImageView];
        //设置尺寸
        CGFloat w = FLNScreenW;
        CGFloat h = 0;
        
        if (item.w) {
            h = w / item.w * item.h;
        }
        
        if (h > FLNScreenH * 0.7) {
            h = FLNScreenH * 0.85;
        }
        
        adImageView.frame = CGRectMake(0, 0, w, h);
        
        //设置图片
        [adImageView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        
        //添加一个手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        adImageView.userInteractionEnabled = YES;
        [adImageView addGestureRecognizer:tap];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        NSLog(@"%@", error);
        
    }];


}
- (void)tap {
    //用safari打开网页
    NSURL *url = [NSURL URLWithString:_item.ori_curl];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }

}

- (IBAction)jump {
    //进入到主框架界面
    FLNViewController *tabBarVc = [[FLNViewController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
    
    [_timer invalidate];
}


- (void)setUpBG {
    
    UIImage *image = nil;
    if (iPhone6P) {
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (iPhone6) {
        
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }else if (iPhone5) {
        
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    }else if (iPhone4) {
        
        image = [UIImage imageNamed:@"LaunchImage-700"];
    }

    _bgView.image = image;

}

@end
